/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.ScreenEngine;
import core.Settings;
import network.Connection;
import view.additional.CellItem;
import view.additional.MenuList;

/**
 *
 * @author Kvest
 */
public class StartMenu extends BasicForm implements MenuList.MenuListSelection 
{
    public static final int ENTER_ID = 0;
    public static final int CALL_SIGN_ID = 1;
    public static final int CLEAR_MEM_ID = 2;
    public static final int VIEW_ID = 3;
    public static final int EDIT_NOTIF_ID = 4;
    public static final int SYSTEM_SETTINGS_ID = 5;
    public static final int INFO_ID = 6;
    public static final int CONTACTS_ID = 7;
    
    public static final int EXIT_COMMAND_ID = 1;
    public static final int OK_COMMAND_ID = 2;
    
    private MenuList items;
   
    private MainForm mainForm;
    private CallsignForm callsignForm;
    private ClearMemoryForm clearMemoryForm;
    private ViewEditForm viewEditForm;
    private NotificationsListForm notificationsListForm;
    private SystemSettingsForm systemSettingsForm; 
    private InfoForm infoForm;
    private ContactsForm contactsForm; 
    
    public StartMenu()
    {
        super("start_menu");
                
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);

        //Заполняем меню
        items = new MenuList();
        items.addItem(new CellItem(ENTER_ID, "enter"));
        items.addItem(new CellItem(CALL_SIGN_ID, "call_sign"));
        items.addItem(new CellItem(CLEAR_MEM_ID, "clear_mem"));
        items.addItem(new CellItem(VIEW_ID, "view"));
        items.addItem(new CellItem(EDIT_NOTIF_ID, "edit_notif"));
        items.addItem(new CellItem(SYSTEM_SETTINGS_ID, "system_settings"));
        items.addItem(new CellItem(INFO_ID, "info"));
        items.addItem(new CellItem(CONTACTS_ID, "contacts"));
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        Command exit = new Command("exit", EXIT_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {                
                IMAPMIDlet.getMidlet().destroyApp(true);
                
                evt.consume();
            }
        };
        addCommand(exit);
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void itemSelected(int id)
    {
        switch(id)
        {
            case ENTER_ID : showMainForm(); break;
            case CALL_SIGN_ID : showCallsignForm(); break; 
            case CLEAR_MEM_ID : showClearMemoryForm(); break;
            case VIEW_ID : showViewEditForm(); break;
            case EDIT_NOTIF_ID : showNotificationsListForm(); break;
            case SYSTEM_SETTINGS_ID: showSystemSettingsForm(); break;
            case INFO_ID : showInfoForm(); break;   
            case CONTACTS_ID : showContactsForm(); break;
        }
    }
    
    private void showNoAuthDataForm()
    {   
        ScreenEngine.getEngine().showMessageBox("settings_error", (new Label("settings_error_text")).getText(), 0);
    }
    
    private void showMainForm()
    {
        //Проверяем наличие логина и пароля
        if (!Settings.getSettings().hasAuthData())
        {
            showNoAuthDataForm();
            return;
        }
                
        if (mainForm == null)
        {
            mainForm = new MainForm();
        }
        
        //Удаляем текущую форму
        ScreenEngine.getEngine().pop(false);
        
        //Отображаем главную форму
        ScreenEngine.getEngine().push(mainForm);
        
        //Начинаем соединение
        Connection.getConnection().start(Settings.getSettings().getServerIP());
    }
    
    private void showCallsignForm()
    {
        if (callsignForm == null)
        {
            callsignForm = new CallsignForm();
        }
        
        ScreenEngine.getEngine().push(callsignForm);
    }
    
    private void showClearMemoryForm()
    {
        if (clearMemoryForm == null)
        {
            clearMemoryForm = new ClearMemoryForm();
        }
        
        ScreenEngine.getEngine().push(clearMemoryForm);
    }
    
    private void showViewEditForm()
    {
        if (viewEditForm == null)
        {
            viewEditForm = new ViewEditForm();
        }
        
        ScreenEngine.getEngine().push(viewEditForm);
    }
    
    private void showNotificationsListForm()
    {
        if (notificationsListForm == null)
        {
            notificationsListForm = new NotificationsListForm();
        }
        
        ScreenEngine.getEngine().push(notificationsListForm);
    }
    
    private void showSystemSettingsForm()
    {
        if (systemSettingsForm == null)
        {
            systemSettingsForm = new SystemSettingsForm();
        }
        
        ScreenEngine.getEngine().push(systemSettingsForm);
    }
    
    private void showInfoForm()
    {
        if (infoForm == null)
        {
            infoForm = new InfoForm();
        }
        
        ScreenEngine.getEngine().push(infoForm);
    }
    
    private void showContactsForm()
    {
        if (contactsForm == null)
        {
            contactsForm = new ContactsForm();
        }
        
        ScreenEngine.getEngine().push(contactsForm);
    }
}
